<!DOCTYPE html>
<html lang="zh-cmn-Hans">

<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="format-detection" content="telephone=no" />
    <link rel="icon" type="image/png" href="./static/favicon.ico" />
    <script src="./static/js/jquery/jquery/2.1.4/jquery.min.js"></script>

    <title>授权登录中.....</title>
    <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
    <script src="./static/js/jquery/jquery.cookie.js"></script>
    <!--<script src="./static/mobile/default/js/page.js"></script>-->


</head>

<body>
    <script>
        $(function () {
            checkLogin()
        });
        //判断是否需要微信授权登录
        function checkLogin() {
            
            var token = $.cookie('code')
            var authorize = getUrlParam('code')
            var test =  getUrlParam('promoterId')
            if(getUrlParam('promoterId')){
                $.cookie('promoterId',getUrlParam('promoterId'))
            }
            if (token == 'null' ||!token) {
                if (!authorize) {
                    $.ajax({
                        url: 'https://mp.jianguoes.com/api/mp/loginMp',
                        type: 'get',
                        headers: { 'token': $.cookie('code') },
                        dataType: 'json',
                        success: function (res) {

                            console.log(res);
                            if (res.errno != 0) {
                                _alert(res.msg);
                                return false
                            }
                            window.location.href = res.data
                        }, error: function () {

                        }
                    });
                } else {

                    $.ajax({
                        url: 'https://mp.jianguoes.com/api/mp/loginMpCallBack',
                        type: 'get',
                        data: { 'code': authorize ,'state':$.cookie('promoterId')},
                        headers: { 'token': $.cookie('code') },
                        dataType: 'json',
                        success: function (res) {
                            if (res.errno == 0) {
                                $.cookie('code', res.data.userVo.weixin_openid)
                                $.cookie('myID', res.data.userVo.userId)
                                window.location.href = './index.html'
                            }

                        }, error: function () {
                            _alert('系统繁忙，请稍后重试')
                        }
                    });
                }
            }

        }
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(r[2]); return null; //返回参数值
        }
        //弹出窗口定义
function _alert(title, fun) {
    var obj = $(".alertBox");

    obj.find(".text").html(title);
    if (typeof fun == "undefined") {
        obj.find(".button").click(function () {
            obj.hide();
        });
        obj.find(".button").show();
        obj.find(".buttonBox").hide();
    } else if (typeof fun == "string") {
        obj.find(".button").click(function () {
            window.location.href = fun;
        });
        obj.find(".button").show();
        obj.find(".buttonBox").hide();
    } else {
        obj.find(".button").hide();
        obj.find(".buttonBox").show();
        obj.find(".cancel").click(function () {
            obj.find(".confirm").unbind("click");
            obj.hide();
        });
        obj.find(".confirm").off("click");
        obj.find(".confirm").on("click", function () {
            obj.hide();
            return fun();
        });
    }
    obj.show();
}
    </script>


</body>

</html>